package com.homework.controller;

import com.alibaba.fastjson.JSON;
import com.homework.entity.ResponseResult;
import com.web.entity.User;
import com.web.service.UserService;
import com.web.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author: XiangHu
 * @date: 2025年03月02日 23:53
 * @describe:
 */
@WebServlet("/login")
public class UserLogin extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("UTF-8");

        resp.setCharacterEncoding("UTF-8");

        resp.setContentType("application/json;charset=UTF-8");

        String username = req.getParameter("username");
        String password = req.getParameter("password");

        UserService userService = new UserServiceImpl();
        User user = userService.login(username, password);

        ResponseResult result = new ResponseResult(200,"OK",user);

        resp.getWriter().write(JSON.toJSONString(result));
    }
}